package com.example.tt_bells_v02;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
//import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wheel.ArrayWheelAdapter;
import com.wheel.WheelView;

//TODO: rework using WheelViewCaptioned
public class WheelTimeView extends LinearLayout {
//public class WheelTimeView extends RelativeLayout {
	
	final float dp_scale = getResources().getDisplayMetrics().density;
	public final WheelView wh_hour, wh_minute;
	public final LinearLayout lay_hour, lay_minute, lay_time;
	public final TextView name_hour, name_minute, name_caption;
	String hours[] = new String[24];
	String minutes[] = new String[60];
	
	public String caption;
	
	public int dpToPx(int dp) {
		return (int)(dp*dp_scale+0.5f);
	}
	
	int fID = 0;
	
	public int findUnusedId() {
	    while( findViewById(++fID) != null );
	    return fID;
	}
	
	public WheelTimeView(Context context,AttributeSet attr) {
		super(context,attr);
		int i;
		
		setOrientation(VERTICAL);
		
		for (i=0; i<24; i++) {
			minutes[i] = hours[i] = Integer.toString(i);
		}
		for (i=24; i<60; i++) {
			minutes[i] = Integer.toString(i);
		}
		
		caption = attr.getAttributeValue("http://schemas.android.com/apk/res/android", "text");
		Log.d("asdfasdf", "GET caption:"+caption);
		
		name_caption = new TextView(context);
		name_caption.setText(caption);
		//name_caption.setId(findUnusedId());
		LayoutParams par_capt = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,1);
		addView(name_caption,par_capt);
		
		//time holder
		lay_time = new LinearLayout(context);
		lay_time.setOrientation(LinearLayout.HORIZONTAL);
		LayoutParams par_th = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,1);
		addView(lay_time,par_th);
		
		//hours wheel with caption
		lay_hour = new LinearLayout(context);
		lay_hour.setOrientation(LinearLayout.VERTICAL);
		LayoutParams par_lh = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1);
		par_lh.setMargins(dpToPx(3), dpToPx(2), dpToPx(3), dpToPx(2));
		lay_time.addView(lay_hour, par_lh);
		
		name_hour = new TextView(context);
		name_hour.setText("Hour:");
		LinearLayout.LayoutParams par_nh = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1);
		lay_hour.addView(name_hour,par_nh);
		
		LinearLayout.LayoutParams par_h = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1);
		//par_h.setMargins(dpToPx(3), dpToPx(2), dpToPx(3), dpToPx(2));
		wh_hour = new WheelView(context);
		lay_hour.addView(wh_hour, par_h);
		
		//LayoutParams par_wh = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1);
		//par_wh.setMargins(dpToPx(3), dpToPx(2), dpToPx(3), dpToPx(2));
		//addView(lay_hour, par_wh);
		
		//minutes wheel
		lay_minute = new LinearLayout(context);
		lay_minute.setOrientation(LinearLayout.VERTICAL);
		LayoutParams par_lm = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1);
		par_lm.setMargins(dpToPx(3), dpToPx(2), dpToPx(3), dpToPx(2));
		lay_time.addView(lay_minute, par_lm);
		
		name_minute = new TextView(context);
		name_minute.setText("Minute:");
		LinearLayout.LayoutParams par_nm = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1);
		lay_minute.addView(name_minute,par_nm);
		
		LinearLayout.LayoutParams par_m = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1);
		wh_minute = new WheelView(context);
		lay_minute.addView(wh_minute, par_m);
				
		ArrayWheelAdapter<String> adapter_h = new ArrayWheelAdapter<String>(context,hours);
		ArrayWheelAdapter<String> adapter_m = new ArrayWheelAdapter<String>(context,minutes);
		
		wh_hour.setViewAdapter(adapter_h);
		wh_hour.setVisibleItems(5);
		wh_hour.setCyclic(true);
		wh_minute.setViewAdapter(adapter_m);
		wh_minute.setVisibleItems(5);
		wh_minute.setCyclic(true);
		
	}
	
	public int getHour() {
		return wh_hour.getCurrentItem();
	}

	public int getMinute() {
		return wh_minute.getCurrentItem();
	}
	
	public void setHour(int new_h) {
		if ((new_h >= 0) && (new_h <=23)) 
			wh_hour.setCurrentItem(new_h);
	}
	
	public void setMinute(int new_m) {
		if ((new_m >= 0) && (new_m <=59)) 
			wh_minute.setCurrentItem(new_m);
	}
}
